What is Float in C++? 您所在的位置:网站首页 float in What is Float in C++?

What is Float in C++?

2023-04-07 13:40| 来源: 网络整理| 查看: 265

The float in C++ is the fundamental primitive built-in data type. Float is the term used for the numerical implementation of floating point numbers. The memory size of float in C++ is 32 bits(i.e. 4 bytes), and there is a variety of functions to manipulate and work with the floating-point numbers.

The float in C++ has a precision only up to the 7 digits, if we exceed its precision limit then it displays the garbage values after the precision limit. The range of float type in the C++ can represents values ranging from approximately 1.5∗10−45{1.5 * 10^{-45}}1.5∗10−45 to 3.4∗1038{3.4 * 10^{38}}3.4∗1038. If we want to store the number with higher precision values than the given range of float, then we can use the double which has a higher range than the float.

Uses of Float

The float values are better in performance as compared to the double values. Processing of float values is faster due to their internal implementation, float value consumes half of the memory bandwidth when compared with the double, due to this reason it is a good decision to use the float more often when the requirement is equal to or less than of 7 digit precision, over the double.

As graphic libraries require extremely high-demand processing power, float values are also preferred in the graphics library due to their performance advantage over double values.

Programmers also prefer to store the records of currencies in the float values due to their ability to define the number of decimal places with some additional parameters.

Float vs Double and Int

Float values and double values both serve the same purpose of storing the floating point values. The main difference between this two is their range and memory size. The size of double in C++ is 64 bits(i.e. 8 bytes) which is twice of float data type. If we want to store the values with a precision of 15 to 16 decimal numbers then double is the preferred choice over the float in C++.

The int is known as the built-in integer data type which is used to store the whole numbers. It serves a completely different purpose than the float. The size of int in C++ is 32 bits(i.e. 4 bytes).

Example 1: Float in C++

Consider the following example where the variable of type float is created and printed using C++.

Coding Example -

#include using namespace std; int main() { // Creating variable of type float float numFloat = 5.6937; // Printing the variable of type float cout


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有